package com.dzy.chiyan.activity

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import com.dzy.chiyan.R
import com.dzy.chiyan.fragment.ChatFragment
import com.dzy.chiyan.fragment.FriendFragment
import com.dzy.chiyan.fragment.SettingFragment
import com.google.android.material.bottomnavigation.BottomNavigationView
// 负责切换底部导航对应的Fragment
class UserActivity : BaseActivity() {
    private lateinit var bottomNavigation: BottomNavigationView // 底部导航栏视图
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user)
        // 获取传递的用户ID
        val userId = intent.getIntExtra("userID", 0)
        Log.d("UserActivity", "Received userId: $userId")
        // 初始化界面，默认加载聊天界面
        loadFragment(ChatFragment(userId))
        bottomNavigation = findViewById(R.id.bottomNav)
        setupBottomNavigation(userId)
    }
    // 点击底部导航栏
    private fun setupBottomNavigation(userId: Int) {
        bottomNavigation.setOnItemSelectedListener { menuItem ->
            when (menuItem.itemId) {
                R.id.chat -> {
                    loadFragment(ChatFragment(userId)) // 切换到聊天界面
                    true
                }
                R.id.friends -> {
                    loadFragment(FriendFragment(userId)) // 切换到好友界面
                    true
                }
                R.id.settings -> {
                    loadFragment(SettingFragment(userId)) // 切换到设置界面
                    true
                }
                else -> {
                    false // 其他未知选项，返回false
                }
            }
        }
    }
    private fun loadFragment(fragment: Fragment) {
        val transaction = supportFragmentManager.beginTransaction()
        // 替换容器内容为指定的Fragment
        transaction.replace(R.id.container, fragment)
        transaction.commit() // 提交事务
    }
}
